package com.zh.note.huawei.twohundred.字母计数or字符统计及重排;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: zhanghuan
 * @date: 2023/9/3 23:16
 * @description: create it
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // String line = sc.nextLine();
        String line = "xyxyXX";

        Map<Character, Integer> map = new HashMap<>();
        char[] chars = line.toCharArray();
        for (char ch : chars) {
            map.put(ch, map.getOrDefault(ch, 0) + 1);
        }
        List<Map.Entry<Character, Integer>> collect = map.entrySet().stream().sorted((o1, o2) -> {
            char char1 = o1.getKey();
            char char2 = o2.getKey();
            int time1 = o1.getValue();
            int time2 = o2.getValue();
            if (time1 != time2) {
                return Integer.compare(time2, time1);
            }
            //相同的话
            if (char1 > 'a' && char2 < 'a' || char1 < 'a' && char2 > 'a') {
                return char2 - char1;
            } else {
                return char1 - char2;
            }
        }).collect(Collectors.toList());

        collect.forEach(item -> {
            System.out.print(item.getKey() + ":" + item.getValue() + ";");
        });

    }
}
